package com.yuerlive.rainbow.module.live.presenter;

import com.yuerlive.rainbow.api.LiveApi;
import com.yuerlive.rainbow.api.LiveService;
import com.yuerlive.rainbow.api.SubscribeApi;
import com.yuerlive.rainbow.api.SubscribeService;
import com.yuerlive.rainbow.api.UserApi;
import com.yuerlive.rainbow.api.UserService;
import com.yuerlive.rainbow.base.BasePresenterImpl;
import com.yuerlive.rainbow.entity.BaseData;
import com.yuerlive.rainbow.entity.DayTaskData;
import com.yuerlive.rainbow.entity.GameInfo;
import com.yuerlive.rainbow.entity.MissionData;
import com.yuerlive.rainbow.entity.User;
import com.yuerlive.rainbow.entity.UserInfo;
import com.yuerlive.rainbow.rx.RxHelper;
import com.yuerlive.rainbow.rx.RxObserver;
import com.yuerlive.rainbow.utils.UserProxy;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoyi on 2017/6/1.
 */

public class ChatRoomPresenter extends BasePresenterImpl implements ChatRoomContract.Presenter {
    private ChatRoomContract.View view;
    private UserService userService;
    private SubscribeService subscribeService;
    private LiveService liveService;

    public ChatRoomPresenter(ChatRoomContract.View view) {
        this.view = view;
        this.view.setPresenter(this);
        subscribeService = SubscribeApi.getInstance().service;
        userService = UserApi.getInstance().service;
        liveService = LiveApi.getInstance().liveService;
    }

    @Override
    public void subscribeUp(String upId) {
        view.showLoading();
        Map<String, String> params = new HashMap<>();
        User user = UserProxy.getUser();

        params.put("upUserId", upId);
        subscribeService.subscribe(params).compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData>(this) {
                    @Override
                    protected void onSuccess(BaseData baseData) {
                        view.hideLoading();
                        view.showSubscribeResult(baseData.getCode(), upId);
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }

    @Override
    public void getUserInfo(String userId) {
        view.showLoading();
        Map<String, String> params = new HashMap<>();
        params.put("otherId", userId);
        userService.getUserInfo(params).compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData<UserInfo>>(this) {
                    @Override
                    protected void onSuccess(BaseData<UserInfo> anchorBaseData) {
                        view.hideLoading();
                        view.showUserInfo(anchorBaseData);
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }

    @Override
    public void getDaily(int num) {
        Map<String, String> params = new HashMap<>();
        User user = UserProxy.getUser();
        params.put("sweetNum", "" + num);
        liveService.getDaily(params)
                .compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData>(this) {
                    @Override
                    protected void onSuccess(BaseData liveDetailDataBaseData) {
                        view.getDailyResult(num, liveDetailDataBaseData);
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }

    @Override
    public void inform(String upUserId, String type) {
        Map<String, String> params = new HashMap<>();
        params.put("upUserId", upUserId);
        params.put("type", type);
        subscribeService.inform(params).compose(RxHelper.toUI()).
                subscribe(new RxObserver<BaseData>(this) {
                    @Override
                    protected void onSuccess(BaseData baseData) {
                        view.showInformResult(baseData.getCode());
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }

    @Override
    public void mute(String liveId, String visitorUserId) {
        HashMap<String, String> params = new HashMap<>();
        params.put("liveId", liveId);
        params.put("visitorUserId", visitorUserId);
        subscribeService.mute(params).compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData>(this) {
                    @Override
                    protected void onSuccess(BaseData baseData) {
                        view.showMuteResult(baseData);
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }

    @Override
    public void getGameList() {
        liveService.getGameList().compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData<List<GameInfo>>>(this) {
                    @Override
                    protected void onSuccess(BaseData<List<GameInfo>> listBaseData) {
                        view.setGameList(listBaseData);
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }

    @Override
    public void getMissionList() {
        liveService.getMissionList().compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData<List<DayTaskData>>>(this) {
                    @Override
                    protected void onSuccess(BaseData<List<DayTaskData>> dayTaskDataBaseData) {
                        view.showMissionList(dayTaskDataBaseData);
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }

    @Override
    public void getMissonReward(DayTaskData dayTaskData) {
        HashMap<String, String> params = new HashMap<>();
        params.put("missionId", dayTaskData.getId()+"");
        liveService.getMissionReward(params).compose(RxHelper.toUI())
              .subscribe(new RxObserver<BaseData<MissionData>>(this) {
                  @Override
                  protected void onSuccess(BaseData<MissionData> data) {
                    view.showMissionReward(data,dayTaskData);
                  }

                  @Override
                  protected void onFail(String message) {

                  }
              });
    }

    @Override
    public void shareLivingRequest(int liveId) {
        HashMap<String, String> params = new HashMap<>();
        params.put("liveId", liveId+"");
        liveService.shareLivingRequet(params).compose(RxHelper.toUI()).
                subscribe(new RxObserver<BaseData>(this) {
                    @Override
                    protected void onSuccess(BaseData data) {
                     view.showShareLivingResult(data);
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }

}
